﻿jQuery(document).ready(function ($) 
{
    $('#carousel').owlCarousel({
        items: 3,
        //autoPlay: 3000,
        stopOnHover: true,
        //autoHeight: true,
        //navigation: true,
        jsonPath: '/student/Feedbacks/-1',// +  $('#courseSelElement').val(),
        jsonSuccess: customDataSuccess
    });

    $(".btn").button();
    $("input.check").button();

    $(".feedbackreloader").click(function (event) {

        $('div.feedbackholder').addClass('gamma-loading');

        var owl = $("#carousel").data('owlCarousel');
        var newOptions = {
            items: 3,
            //autoPlay: 3000,
            stopOnHover: true,
            //autoHeight: true,
            //navigation: true,
            jsonPath: '/student/Feedbacks/-1',// +  $('#courseSelElement').val(),
            jsonSuccess: customDataSuccess
        };
        owl.reinit(newOptions);

        //$.get('/student/Feedbacks/-1', null, customDataSuccess);
        event.preventDefault();
    });


    $('.autoplay').click(function (event) {
        var owl = $("#carousel");//.data('owlCarousel');
        if ($(this).is(':checked'))
            owl.trigger('owl.play', 3000);
        else
            owl.trigger('owl.stop');
        //event.preventDefault();
    });

    //$('#carousel').on('click','a.showImageT',function () {
    //    var imgUrl = $(this).attr('data-src');
    //    Gamma.singleviewitempublic(imgUrl);
    //});

    //$('.fancybox')

    //$.getJSON('http://blog.kharkovitcourses.com/Home/GetJson', {})
    //    .done(function (data) {
    //        $('ul#newsList').empty();
    //        $.each(data, function (i, val) {

    //            var href = "http://blog.kharkovitcourses.com/" + val.PostYear + '/' + val.PostMonth + '/' + val.PostUrl;

    //            $('ul#newsList').append('<li><a href="' + href + '"><span class="tab">' + val.PostTitle + '</span></a></li>');
    //        });
    //    })
    //    .fail(function () {
    //        console.log("error");
    //    });

    //var GammaSettings = {
    //    // order is important!
    //    viewport : [ {
    //        width : 1200,
    //        columns : 5
    //    }, {
    //        width : 900,
    //        columns : 4
    //    }, {
    //        width : 500,
    //        columns : 3
    //    }, {
    //        width : 320,
    //        columns : 2
    //    }, {
    //        width : 0,
    //        columns : 2
    //    }],
    //    columns: 3,
    //    historyapi: false
    //};
    //Gamma.init(GammaSettings);
});

function customDataSuccess(data) {
    var content = "";
    for (var i in data["rows"]) {

        if (data.rows[i].VKProfileRef)
        {
            content += "<div id=item" + i + " class='item' vkref=" + data.rows[i].VKProfileRef + "> <a href='/Training/Course/"
                + data.rows[i].CourseRef + "' class='coursename'> " + data.rows[i].CourseName + "</a> <p>\"" + data.rows[i].FeedBack + "\"</p>";
                //+ "<a class='fancybox' href='http://kharkovitcourses.com/Images/header_design-850x330.jpg'>ещё...</a>"
                //+ "<div class='vkHolder' id='vkHolder" + i + "'></div> </div>";
            if (data.rows[i].ImageRef)
            {
                content += "<a class='fancybox' href='" + data.rows[i].ImageRef + "'>ещё...</a>";
            }
            content += "<div class='vkHolder' id='vkHolder" + i + "'></div> </div>";
        }
        else
        {
            content += "<div id=item" + i + " class='item'> <a href='/Training/Course/"
                + data.rows[i].CourseRef + "' class='coursename'>" + data.rows[i].CourseName + " </a><p>" + data.rows[i].FeedBack + "\" </p>";
            if (data.rows[i].ImageRef) {
                content += "<a class='fancybox' href='" + data.rows[i].ImageRef + "'>ещё...</a>";
            }
            content += "<p class='unlinkedPerson'><img src='/Images/person22.png'/>" + data.rows[i].StudentName + "</p> </div>";
        }
    }

    $("#carousel").html(content);

    $("div[vkref]", "#carousel").each(function (i, element) {
        $('<script>')
          .attr('type', 'text/javascript')
          .text('VK.Widgets.Subscribe("' + $('.vkHolder', element).attr('id') + '", {}, ' + $(element).attr('vkref') + ');')
          .appendTo($('.vkHolder', element));
    });

    $(".fancybox").fancybox({
		openEffect	: 'none',
		closeEffect	: 'none'
    });

    $('div.feedbackholder').removeClass('gamma-loading');
}
